package server

import (
	"context"
	"srv-server/basic/global"
	__ "srv-server/basic/proto"
	"srv-server/handler/model"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	__.UnimplementedSrvServer
}

// SayHello implements helloworld.GreeterServer
// func (s *Server) SayHello(_ context.Context, in *__.HelloRequest) (*__.HelloReply, error) {
//
//		return &__.HelloReply{Message: "Hello " + in.GetName()}, nil
//	}
func (s *Server) UploadFile(_ context.Context, in *__.UploadFileRequest) (*__.UploadFileResponse, error) {
	var m model.Mediumfile
	global.DB.Model(model.Mediumfile{}).Where("file_uuid = ?", in.FileUuid).Find(&m)
	if m.Id != 0 {
		return &__.UploadFileResponse{
			Code: 501,
			Msg:  "该文件已存在",
		}, nil
	}
	if in.FileType == 1 {
		m = model.Mediumfile{

			FileUuid: in.FileUuid,
			Image:    in.Image,

			FileType: uint(in.FileType),
			FileSize: float64(in.FileSize),
		}
	} else if in.FileType == 2 {
		m = model.Mediumfile{

			FileUuid: in.FileUuid,
			Image:    in.Image,

			FileType: uint(in.FileType),
			FileSize: float64(in.FileSize),
		}
	} else {
		return &__.UploadFileResponse{
			Code: 501,
			Msg:  "暂不支持上传",
		}, nil
	}

	if err := global.DB.Create(&m).Error; err != nil {
		return &__.UploadFileResponse{
			Code: 500,
			Msg:  "上传文件失败",
		}, nil
	}
	return &__.UploadFileResponse{
		Code: 200,
		Msg:  "上传文件成功",
	}, nil
}
func (s *Server) FindFile(_ context.Context, in *__.FindFileRequest) (*__.FindFileResponse, error) {
	var md []model.Mediumfile

	global.DB.Model(model.Mediumfile{}).Where("file_uuid = ?", in.Id).Find(&md)
	var m []*__.Findfile
	for _, i := range md {
		findfile := __.Findfile{
			FileUuid: i.FileUuid,
			Image:    i.Image,
			Video:    i.Video,
			FileType: int64(i.FileType),
			FileSize: float32(i.FileSize),
		}
		m = append(m, &findfile)
	}
	return &__.FindFileResponse{
		Code: 200,
		Msg:  "唯一标识查询成功",
		List: m,
	}, nil
}
func (s *Server) GetFile(_ context.Context, in *__.GetFileRequest) (*__.GetFileResponse, error) {
	var md []model.Mediumfile
	if in.FileType != 0 {
		global.DB.Model(model.Mediumfile{}).Where("file_type = ?", in.FileType).Find(&md)
	} else if in.FileSize != 0 {
		global.DB.Model(model.Mediumfile{}).Where("file_size = ?", in.FileSize).Find(&md)
	}

	var m []*__.GetFile
	for _, i := range md {
		findfile := __.GetFile{
			FileUuid: i.FileUuid,
			Image:    i.Image,
			Video:    i.Video,
			FileType: int64(i.FileType),
			FileSize: float32(i.FileSize),
		}
		m = append(m, &findfile)
	}
	return &__.GetFileResponse{
		List: m,
	}, nil
}
